package org.dromara.system;

import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.ColumnConfig;
import com.mybatisflex.codegen.config.EntityConfig;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.dromara.common.mybatis.core.domain.BaseDO;

public class Codegen {

    public static void main(String[] args) {
        //配置数据源
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/ms-vue?characterEncoding=utf-8");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");

        //创建配置内容，两种风格都可以。
        GlobalConfig globalConfig = createGlobalConfigUseStyle1();
        //GlobalConfig globalConfig = createGlobalConfigUseStyle2();

        //通过 datasource 和 globalConfig 创建代码生成器
        Generator generator = new Generator(dataSource, globalConfig);

        //生成代码
        generator.generate();
    }

    public static GlobalConfig createGlobalConfigUseStyle1() {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        //设置根包
        globalConfig.setBasePackage("com.test");
        //设置表前缀和只生成哪些表
//        globalConfig.setTablePrefix("sys_");
//        globalConfig.setGenerateTable("tb_account", "tb_account_session");

//        globalConfig.setTemplateEngine(new ITemplate() {
//            @Override
//            public void generate(Map<String, Object> params, String templateFilePath, File generateFile) {
//                System.out.println(JsonUtils.toJsonString(params));
//                System.out.println(templateFilePath);
//                System.out.println(generateFile.getAbsolutePath());
//            }
//        });


        globalConfig.enableEntity()
            .setJdkVersion(21)
            .setWithLombok(true)
            .setClassSuffix("DO")
            .setSuperClass(BaseDO.class)
            .setWithSwagger(true)
            .setSwaggerVersion(EntityConfig.SwaggerVersion.DOC);

        globalConfig.enableMapper();
        globalConfig.enableMapperXml();
        globalConfig.enableService();
        globalConfig.enableServiceImpl();
        globalConfig.enableController();


        //可以单独配置某个列
        ColumnConfig columnConfig1 = new ColumnConfig();
        columnConfig1.setColumnName("del_flag");
        columnConfig1.setLogicDelete(true);
        globalConfig.setColumnConfig(columnConfig1);
        //忽略字段
        globalConfig.getStrategyConfig().setIgnoreColumns("createBy", "createTime", "updateBy", "updateTime");


        return globalConfig;
    }
}
